!ifndef UTILS
!define UTILS

Function IndexOf
  Exch $R0
  Exch
  Exch $R1
  Push $R2
  Push $R3

   StrCpy $R3 $R0
   StrCpy $R0 -1
   IntOp $R0 $R0 + 1
  StrCpy $R2 $R3 1 $R0
  StrCmp $R2 "" +2
  StrCmp $R2 $R1 +2 -3

   StrCpy $R0 -1

   Pop $R3
   Pop $R2
   Pop $R1
   Exch $R0
FunctionEnd

!macro IndexOf Var Str Char
  Push "${Char}"
  Push "${Str}"
   Call IndexOf
  Pop "${Var}"
!macroend




var major_1
var minor1_1
var major_2
var minor1_2

Function CompareVersion ; 0 - stejne verze, 1 - je nainstalovana starsi verze, 2 - je nainstalovana novejsi verze
  pop $R0
  pop $R1

/* Major verze */
  !insertmacro IndexOf $0 "$R0" "."
  strCpy $major_1 $R0 $0 0
  strlen $7 $R0
  intop $7 $7 - $0
  intop $7 $7 - 1
  intop $0 $0 + 1
  strCpy $R0 $R0 $7 $0

  !insertmacro IndexOf $0 "$R1" "."
  strCpy $major_2 $R1 $0 0
  strlen $7 $R1
  intop $7 $7 - $0
  intop $7 $7 - 1
  intop $0 $0 + 1
  strCpy $R1 $R1 $7 $0

/* minor verze */
  strCpy $minor1_1 $R0
  strCpy $minor1_2 $R1

/* Porovnani */
  ${if} $major_1 == $major_2
    ${if} $minor1_1 == $minor1_2
      push 0
    ${elseif} $minor1_1 > $minor1_2
      push 1
    ${elseif} $minor1_1 < $minor1_2
      push 2
    ${endif}
  ${elseif} $major_1 > $major_2
    push 1
  ${elseif} $major_1 < $major_2
    push 2
  ${endif}
  ;MessageBox MB_ICONINFORMATION|MB_OK 'Instalujici: $major_1.$minor1_1 Instalovana: $major_2.$minor1_2'
FunctionEnd

!macro CompareAppVersion var installedVer installingVer
  push "${installedVer}"
  push "${installingVer}"

  call CompareVersion

  pop "${var}"
!macroEnd



; .NET Framework
!define MIN_FRA_MAJOR "3"
!define MIN_FRA_MINOR "5"
!define MIN_FRA_BUILD "*"
var dotnetinstalled
var dotnetversion

Function VerifyIfBadFramework
  ;Save the variables in case something else is using them
  Push $0
  Push $1
  Push $2
  Push $3
  Push $4
  Push $R1
  Push $R2
  Push $R3
  Push $R4
  Push $R5
  Push $R6
  Push $R7
  Push $R8

  StrCpy $R5 "0"
  StrCpy $R6 "0"
  StrCpy $R7 "0"
  StrCpy $R8 "0.0.0"
  StrCpy $0 0
  StrCpy $dotnetinstalled "yes"

  loop:

  ;Get each sub key under "SOFTWARE\Microsoft\NET Framework Setup\NDP"
  EnumRegKey $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP" $0
  StrCmp $1 "" done ;jump to end if no more registry keys
  IntOp $0 $0 + 1
  StrCpy $2 $1 1 ;Cut off the first character
  StrCpy $3 $1 "" 1 ;Remainder of string

  ;Loop if first character is not a 'v'
  StrCmpS $2 "v" start_parse loop

  ;Parse the string
  start_parse:
  StrCpy $R1 ""
  StrCpy $R2 ""
  StrCpy $R3 ""
  StrCpy $R4 $3

  StrCpy $4 1

  parse:
  StrCmp $3 "" parse_done ;If string is empty, we are finished
  StrCpy $2 $3 1 ;Cut off the first character
  StrCpy $3 $3 "" 1 ;Remainder of string
  StrCmp $2 "." is_dot not_dot ;Move to next part if it's a dot

  is_dot:
  IntOp $4 $4 + 1 ; Move to the next section
  goto parse ;Carry on parsing

  not_dot:
  IntCmp $4 1 major_ver
  IntCmp $4 2 minor_ver
  IntCmp $4 3 build_ver
  IntCmp $4 4 parse_done

  major_ver:
  StrCpy $R1 $R1$2
  goto parse ;Carry on parsing

  minor_ver:
  StrCpy $R2 $R2$2
  goto parse ;Carry on parsing

  build_ver:
  StrCpy $R3 $R3$2
  goto parse ;Carry on parsing

  parse_done:

  IntCmp $R1 $R5 this_major_same loop this_major_more
  this_major_more:
  StrCpy $R5 $R1
  StrCpy $R6 $R2
  StrCpy $R7 $R3
  StrCpy $R8 $R4

  goto loop

  this_major_same:
  IntCmp $R2 $R6 this_minor_same loop this_minor_more
  this_minor_more:
  StrCpy $R6 $R2
  StrCpy $R7 R3
  StrCpy $R8 $R4
  goto loop

  this_minor_same:
  IntCmp R3 $R7 loop loop this_build_more
  this_build_more:
  StrCpy $R7 $R3
  StrCpy $R8 $R4
  goto loop

  done:

  ;Have we got the framework we need?
  IntCmp $R5 ${MIN_FRA_MAJOR} max_major_same fail end
  max_major_same:
  IntCmp $R6 ${MIN_FRA_MINOR} max_minor_same fail end
  max_minor_same:
  IntCmp $R7 ${MIN_FRA_BUILD} end fail end

  fail:
  StrCmp $R8 "0.0.0" no_framework
  goto wrong_framework

  no_framework:
     StrCpy $dotnetinstalled "no"
  goto end

  wrong_framework:
     StrCpy $dotnetinstalled "no"
  goto end

  end:
  StrCpy $dotnetversion $R8

  ;Pop the variables we pushed earlier
  Pop $R8
  Pop $R7
  Pop $R6
  Pop $R5
  Pop $R4
  Pop $R3
  Pop $R2
  Pop $R1
  Pop $4
  Pop $3
  Pop $2
  Pop $1
  Pop $0
FunctionEnd

Function InstallFramework
  File /oname=$TEMP\dotNetFx35setup.exe "Data\dotNetFx35setup.exe"

  ExecWait '$TEMP\dotNetFx35setup.exe'
  Delete "$TEMP\dotNetFx35setup.exe"
FunctionEnd

!endif